const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async (event, context) => {
  const { userOpenid, service } = event

  // 获取当前时间
  const now = new Date()
  const year = now.getFullYear()
  const month = String(now.getMonth() + 1).padStart(2, '0')
  const day = String(now.getDate()).padStart(2, '0')
  const hour = String(now.getHours()).padStart(2, '0')
  const minute = String(now.getMinutes()).padStart(2, '0')
  const completeTime = `${year}-${month}-${day} ${hour}:${minute}`

  try {
    const result = await cloud.openapi.subscribeMessage.send({
      touser: userOpenid,
      page: 'pages/myBookings/myBookings',
      data: {
        thing1: { value: service },
        time2: { value: completeTime },
        thing3: { value: '欢迎对本次服务进行评价' }
      },
      templateId: '_ZNW0LfTQWA1qXCkgDOu22XKYYvO5Pa6ZHN-9383lw',
      miniprogramState: 'developer'
    })

    return { success: true, result }
  } catch (err) {
    console.error('发送通知失败:', err)
    return { success: false, error: err }
  }
}
